package com.woniumall.util;

import com.woniumall.dao.GoodsDao;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

/**
 * @auther linke
 * @create 2021-10-18 11:09
 */
public class sqlSessionUtil {
    private static SqlSessionFactory factory;
    static{
        InputStream is = null;
        try {
            //a.获取读取核心配置文件的流对象
            is = Resources.getResourceAsStream("mybatis_config.xml");
        } catch (IOException e) {
            e.printStackTrace();
        }
        SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
        //System.out.println(builder);//org.apache.ibatis.session.SqlSessionFactoryBuilder@694f9431
        //b.获取SqlSession工厂的构建者对象
        factory = builder.build(is);
        //c.获取SqlSession工厂对象
        // System.out.println(factory);//org.apache.ibatis.session.defaults.DefaultSqlSessionFactory@51cdd8a
    }
    public static SqlSession sqlSession() {
        SqlSession sqlSession = factory.openSession();
        //d.获取SqlSession对象
        return sqlSession;
    }
    public static void  close(SqlSession sqlSession){
        sqlSession.close();
    }
}
